#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import os
import subprocess
from .._util import Util
from .._prototype import PreBuildImages as PreBuildImagesBase
from .._workdir import WorkDirAddonBase


class WorkDirAddon(WorkDirAddonBase):

    def __init__(self, parent, osVersion):
        self._parent = parent
        self._osVersion = osVersion

    @property
    def image_filepath(self):
        return os.path.join(self._parent._path, "lineage-21.0-20240628-UNOFFICIAL-TP1803.zip")

    @property
    def recover_image_filepath(self):
        return os.path.join(self._parent._path, "recover.img")

    @property
    def tmp_file(self):
        return os.path.join(self._parent._path, "tmp.file")


class PreBuildImages(PreBuildImagesBase):

    def has_image(os_version):
        if os_version == "19.1":
            # https://xdaforums.com/t/guide-tutorial-howto-sony-xperia-z5-compact-stock-to-android-12.4655815/#post-89333821
            return True
        elif os_version == "12.1":
            # https://github.com/djibe/Android-tutorials?tab=readme-ov-file#sony-xperia-z5-compact
            return True
        return False

    def fetch_image(os_version):
        if os_version == "19.1":
            url = None
        elif os_version == "12.1":
            url = None
        else:
            assert False

        subprocess.check_call(["wget", "--show-progress", "-O", None, url])
